﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Base.Service
{
    public class HostInfoService
    {
        private List<HostInfo> fasbmHostInfos = null;

        private static readonly object syncObject = new object();

        private static HostInfoService instance;

        private HostInfoService()
        {
            Init();
        }

        public static HostInfoService GetInstance()
        {
            if (instance == null)
            {
                lock (syncObject)
                {
                    if (instance == null)
                        instance = new HostInfoService();
                }
            }
            return instance;
        }
        /// <summary>
        /// 根据Host名称获取ip 端口等信息
        /// </summary>
        /// <param name="hostName"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public HostInfo GetHostInfoByHostName(string hostName)
        {
            if (string.IsNullOrEmpty(hostName))
                throw new ArgumentException("构建服务器不能为空！");
            var info = fasbmHostInfos.FirstOrDefault(t => t.HostName == hostName);
            if (info == null)
                throw new Exception($"Host{hostName}信息不正确");
            return info;
        }
        private void Init()
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                fasbmHostInfos = new FASRouterDao().GetHostInfos("FASBM");
            }
        }
    }
}
